/*
* $Id: ViewFamilyWizard.java 272 2011-02-21 05:37:35Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.component.journal;

import java.util.Vector;

import apiary.Apiary;
import apiary.common.FormHelper;
import apiary.db.managers.DBManager;
import apiary.db.FamilyStore.FamilyRecord;
import apiary.db.FieldsValueStore.FieldsValueRecord;
import apiary.settings.SettingStore;
import apiary.settings.SettingsRecord;
import apiary.settings.SettingsRecord.Field;
import apiary.ui.BackScreen;
import apiary.ui.journal.ViewForm;
import apiary.ui.journal.ChooseForm;
import apiary.ui.journal.WizardScreen;
import apiary.ui.journal.ChooseForm.ChooseValue;
import java.util.Hashtable;

/**
 * Wizard of view family
 * @author lexus
 */
public class ViewFamilyWizard extends Wizard
{
    /**
     * Constructor
     */
    public ViewFamilyWizard() {
        super(null);
        Init();
    }

    /**
     * Constructor
     * @param parentForm Form where it want to move when pressing Back.
     * This form used only on first form of wizard
     */
    public ViewFamilyWizard(BackScreen parentForm) {
        super(parentForm);
        Init();
        setCancelForm(parentForm);
    }

    /**
     * Initialize Wizard
     */
    private void Init() {
        Integer idChoose = new Integer(1);
        Apiary apiary = Apiary.getInstance();
        firstForm = new ChooseForm(apiary.getLocaleString("ChoosePoint"), idChoose, this, true);
        FormHelper.fillPointList((ChooseForm) firstForm);
        Integer idFamily = new Integer(2);
        ChooseForm chooseForm = new ChooseForm(apiary.getLocaleString("ChooseFamily"), idFamily, this, true);
        chooseForm.setBackScreen(firstForm);
        formsNext.put(idChoose, chooseForm);
        Integer idView = new Integer(3);
        ViewForm form = new ViewForm(apiary.getLocaleString("ViewPoint"), idView, this);
        form.setBackScreen(chooseForm);
        formsNext.put(idFamily, form);
    }

    /**
     * @deprecated In this wizard don't use
     */
    public void save() {
    }

    /**
     * Calls before to show next form of wizard
     * @param nextForm next form of wizard
     */
    public void next(BackScreen nextForm) {
        WizardScreen form = (WizardScreen) nextForm;
        Integer idForm = form.getIdForm();
        if(idForm.intValue() == 2) {
            ChooseValue point = (ChooseValue) formData.get(new Integer(1));
            FormHelper.fillFamilyList(point.id, (ChooseForm) form);
        } else if(idForm.intValue() == 3) {
            ChooseValue family = (ChooseValue) formData.get(new Integer(2));
            setFamilyData(family.id, (ViewForm) form);
        }
    }

    /**
     * Checks data of form on truth
     * @param dataForm data of form
     * @param idForm identificator of form
     * @return truth of data
     */
    public boolean isOk(Integer idForm, Object dataForm) {
        return true;
    }

    /**
     * Form is filled data family
     * @param idFamily identificator of family
     * @param viewForm form for fill
     */
    private void setFamilyData(int idFamily, ViewForm viewForm) {
        DBManager dBManager = DBManager.getInstance();
        FamilyRecord family = dBManager.getFamily(idFamily);
        Apiary apiary = Apiary.getInstance();
        viewForm.append(apiary.getLocaleString("NameFamily").concat(": "));
        viewForm.append(family.name.concat("\n"));
        Field field = (Field) SettingStore.getInstance().getData(SettingsRecord.TYPE_FAMILY);
        if(field.getValue() == Boolean.TRUE) {
            viewForm.append(apiary.getLocaleString("TypeFamily").concat(": "));
            Hashtable types = DBManager.getInstance().getTypesFamily();
            String type = (String) types.get(new Integer(family.typeFamily));
            viewForm.append(type.concat("\n"));
        }
        field = (Field) SettingStore.getInstance().getData(SettingsRecord.NOTE_FAMILY);
        if(field.getValue() == Boolean.TRUE) {
            viewForm.append(apiary.getLocaleString("Note").concat(": "));
            viewForm.append(family.noteFamily.concat("\n"));
        }
        Vector fieldsValue = dBManager.getFieldsValue(family.idRecord);
        setUserField(viewForm, fieldsValue);
    }

    /**
     * Form is filled user data family
     * @param userFields user data of family
     * @param form form for fill
     */
    private void setUserField(ViewForm form, Vector userFields) {
        for(int i = 0; i < userFields.size(); i++) {
            FieldsValueRecord fvRecord = (FieldsValueRecord) userFields.elementAt(i);
            form.append(fvRecord.nameField.concat(": "));
            form.append(fvRecord.valueField.concat("\n"));
        }
    }
}
